
/*
*Released on 28th June 2008.
*Any violations can be reported at paritosh@wikiocean.net
*What is treated as violations can be found at www.wikiocean.net/ppl1-voilations*
******************************************************************************
* The contents of this file are subject to POOL Public License 1.0 and later.
* POOL Public License is based on Affero GPL v3 and additional conditions.
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the Affero GPL v3 License at http://www.gnu.org/licenses/agpl.html
* You may obtain a copy of the POOL Public License 1.0 or later at www.wikiocean.net/license/ppl.html
* Software distributed under POOL Public License 1.0 is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer of the Original Code is Paritosh Pungaliya (C) 2008. All Rights Reserved.
******************************************************************************
* Objective of the additional terms (license)
* 1) Is to extend the software freedom to freedom to work.
* 2) To ensure that knowledge is free from monopoly of agencies.
* 3) To avoid a situation where big corporate or investor can buy out free software groups and companies and then start milking the communities built around it.
* (this trend can be seen how the freedom is curtailed in companies that get Venture Capital funding.)
******************************************************************************
*/

package  utility;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Hashtable;

import logger.PoolLogger;
import dbmanager.DBManager;

public class Reveresdictionary 
{
	String msg;
	String userid;
	final String DICTIONARY = "dictionary";
	PoolLogger pl;
	
	public Reveresdictionary()
	{	// logger
		pl = (PoolLogger) PoolLogger.getLogger(this.getClass().getName());
	}
	
	/*public String replecesFromDictionary()
	{	
		SplitString sp = new SplitString();
		int index=msg.indexOf("*");
		String temp = msg.substring(0,index);
		String tempmsg = msg.substring(index);
		String SplitString[] =sp.splitString(tempmsg);
		String tempSplit[] = new String[SplitString.length];
		tempSplit[0] = temp;
		Hashtable<Object, Object>table = new Hashtable<Object, Object>();
		table.put("userid",userid);
		for(int i=0;i<tempSplit.length-1;i++)
		{
			tempSplit[i+1] = SplitString[i].replace("\"", "");
			SplitString[i] = tempSplit[i];
			tempSplit[i]= replaceabrevation(table, tempSplit[i]);
			if(msg.contains(SplitString[i]))
				msg =msg.replace(SplitString[i],tempSplit[i]);
		}
		return msg;
	}*/
	
	public String replecesFromDictionary(String msg,String userid)
	{	
		try 
		{
			Hashtable<Object, Object>table = new Hashtable<Object, Object>();
			table.put("userid",userid);
			//ResultSet rs = db.getSelect("Select * from dictionary where userid='"+userid+"'");
			msg = msg.replaceAll("\"","");
			ResultSet rs = DBManager.getSelect("Select msgid,abrevation,length(abrevation) x from dictionary where userid in('"+userid+"','+911234567891') and msgid!='system' order by x desc");
			while(rs.next())
			{
				String abrevation = rs.getString("msgid");
				//String replace= replaceabrevation(table, abrevation);
				if(msg.contains(abrevation))
				{
					String replace = rs.getString("abrevation");
					msg = msg.replace(abrevation, replace);
				}
			}	
		}
		catch (SQLException e) 
		{
			e.printStackTrace();
		}
		return msg;
	}
	
	public String replaceabrevation(Hashtable< Object,Object>table,String Split)
	{
		table.put("msgid", Split);
		ResultSet rs =DBManager.getselect(table, "DICTIONARY");
		try
		{
			if(rs.next())
			{
				Split = rs.getString("abrevation");
				table.put("msgid", Split);
				Split = replaceabrevation(table,Split);
			}	
		}
		catch (SQLException e) 
		{
			System.out.println("SQLException  in checkInCommandabrevation::replecesFromDictionary::Reveresdictionary");
			pl.info("SQLException  in checkInCommandabrevation::replecesFromDictionary::Reveresdictionary");
			pl.debug("SQLException  in checkInCommandabrevation::replecesFromDictionary::Reveresdictionary");
			pl.warn("SQLException  in checkInCommandabrevation::replecesFromDictionary::Reveresdictionary");
			pl.fatal("SQLException  in checkInCommandabrevation::replecesFromDictionary::Reveresdictionary");
			pl.error("SQLException  in checkInCommandabrevation::replecesFromDictionary::Reveresdictionary");
			pl.trace("SQLException  in checkInCommandabrevation::replecesFromDictionary::Reveresdictionary");
			e.printStackTrace();
		}	
	  return Split;
	}
	
	public static void main(String[] args) 
	{
		Reveresdictionary rd = new Reveresdictionary();
		String msg = rd.replecesFromDictionary("11 transaction add*P0[\"2007-10-12\"<\"Trimurti Generals\",AQE15,AGW5,DJU20 {total_amount=1721.25}{$=208}","+919960613969");
		System.out.println("The sms is "+msg);
	}
}
